//
//  NewViewController.m
//  艾听歌
//
//  Created by dllo on 16/5/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "NewViewController.h"
#import "NewCVCell.h"
#import "NewRecommend.h"
#import "NewDetailViewController.h"

#define NewCELL @"CELL"
#define HeaderView @"header"

@interface NewViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *CollectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *NewFlow;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) BOOL isUpLoading;
@property (nonatomic, assign) NSInteger nextpage;

@end

@implementation NewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    self.view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9C9C9C);
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorPickerWithRGB(0xFF6EB4, 0x9C9C9C);
//    self.title = @"123";
    [self getHandleData:self.nextpage];
    [self createCollectionView];
    self.dataArray = [NSMutableArray array];
    [self.CollectionView registerClass:[NewCVCell class] forCellWithReuseIdentifier:NewCELL];
    [self addHeader];
    [self addFooter];
}

- (void)addHeader
{
    __block NewViewController *root = self;
    self.CollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        root.nextpage = 1;
        root.isUpLoading = NO;
        [root getHandleData:root.nextpage];
        
    }];
    [self.CollectionView.mj_header beginRefreshing];
}
- (void)addFooter
{
    __block NewViewController *root = self;
    self.CollectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        root.isUpLoading = YES;
        root.nextpage++;
        [root getHandleData:root.nextpage];
        
    }];
}
- (void)getHandleData:(NSInteger)nextCursor
{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/misc/album/new?page=%ld&size=30&app=ttpod&v=v9.0.1.2016050614&user_id=0&mid=iPhone7%%2C2&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=5e6106cc8d5bcf35f60a2997c79a00bb5a7c631f&idfa=ABDADD65-0877-4840-B14F-DFC8D1114AB0&utdid=VzR0dwnWYs8DANbYJ8SsFzOU&alf=(null)&bundle_id=com.ttpod.music&latitude=-1&longtitude=-1&access_token=(null)&vc=9000100", nextCursor];
    
    
    NSString *newStr = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    
    
    [manager GET:newStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [netWorkManager stopMonitoring];
        if (self.isUpLoading == NO) {
            [self.dataArray removeAllObjects];
        }
        
        NSMutableArray *bigArray = [responseObject objectForKey:@"data"];
        for (NSDictionary *dic in bigArray) {
            NewRecommend *newModel = [[NewRecommend alloc] init];
            [newModel setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:newModel];
        }
        if (nextCursor == 1) {
            [self.dataArray removeObjectAtIndex:0];
        }
 
        [self.CollectionView.mj_footer endRefreshing];
        [self.CollectionView.mj_header endRefreshing];
        
        [self.CollectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}
- (void)createCollectionView
{
    //layout
    self.NewFlow = [[UICollectionViewFlowLayout alloc] init];
    self.NewFlow.minimumInteritemSpacing = 0;
    self.NewFlow.minimumLineSpacing = 10;
    self.NewFlow.sectionInset = UIEdgeInsetsMake(20, 20, 10, 20);
    CGFloat w = (self.view.frame.size.width - 20 - 40) / 2 - 0.001;
    self.NewFlow.itemSize = CGSizeMake(w, 200);
    
    //cv
   self.CollectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:self.NewFlow];
//    self.CollectionView.backgroundColor = [UIColor whiteColor];
    self.CollectionView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9C9C9C);
   self.CollectionView.delegate = self;
    self.CollectionView.dataSource = self;
    [self.view addSubview:self.CollectionView];
    
    //注册cell
    [self.CollectionView registerClass:[NewCVCell class] forCellWithReuseIdentifier:NewCELL];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NewCVCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NewCELL forIndexPath:indexPath];
    cell.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9C9C9C);
    NewRecommend *model = self.dataArray[indexPath.item];

    cell.namelabel.text = model.singerName;
    cell.songLabel.text = model.name;
    [cell.pic sd_setImageWithURL:[NSURL URLWithString:model.picUrl] placeholderImage:[UIImage imageNamed:@"hold.png"]];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NewDetailViewController *detailVC = [[NewDetailViewController alloc] init];
    
    NewRecommend *model = self.dataArray[indexPath.row];
    detailVC.albumId = [model.albumId stringValue];
    
    
    
    [self.navigationController pushViewController:detailVC animated:YES];
}





// 视图将要出现的时候 tabBar隐藏
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    
    
}


@end
